package com.itheima.salarys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.IdWorker;
import com.itheima.salarys.client.AttendanceFeignClient;
import com.itheima.salarys.client.FeignClientService;
import com.itheima.salarys.client.SocialSecurityFeignClient;
import com.itheima.salarys.mapper.SalaryArchiveDetailMapper;
import com.itheima.salarys.mapper.SalaryArchiveMapper;
import com.itheima.salarys.mapper.SettingsMapper;
import com.itheima.salarys.mapper.UserSalaryMapper;
import com.itheima.salarys.pojo.*;
import com.itheima.salarys.service.SalarysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/12/15 21:38
 */
@Service
@Transactional
@SuppressWarnings(value = "all")
public class SalarysServiceImpl implements SalarysService {

    @Autowired
    private SalaryArchiveMapper salaryArchiveMapper;

    @Autowired
    private SalaryArchiveDetailMapper salaryArchiveDetailMapper;

    @Autowired
    private UserSalaryMapper userSalaryMapper;

    @Autowired
    private SettingsMapper settingsMapper;

    @Autowired
    private FeignClientService feignClientService;

    @Autowired
    private IdWorker idWorker;

    @Override
    public SalaryArchive findSalaryArchiveByYearMonthAndCompanyId(String companyId, String yearMonth) {
        QueryWrapper<SalaryArchive> salaryArchiveQuery=new QueryWrapper<SalaryArchive>();
        salaryArchiveQuery.and(i->i.eq("company_id",companyId).eq("years_month",yearMonth));
        SalaryArchive salaryArchive = salaryArchiveMapper.selectOne(salaryArchiveQuery);
        if(salaryArchive==null){
            return null;
        }
        return salaryArchive;
    }


    @Override
    public List<SalaryArchiveDetail> findSalaryArchiveDetailListBySarlaryArciveId(String id) {
        QueryWrapper<SalaryArchiveDetail> salaryArchiveDetailQuery=new QueryWrapper<SalaryArchiveDetail>();
        salaryArchiveDetailQuery.eq("archive_id",id);
        List<SalaryArchiveDetail> salaryArchiveDetailList = salaryArchiveDetailMapper.selectList(salaryArchiveDetailQuery);
        if (CollectionUtils.isEmpty(salaryArchiveDetailList)) {
            return null;
        }
        return salaryArchiveDetailList;
    }

    /**
     * 查询工资的月报表数据
     *
     * 社保归档明细  考勤归档明细
     */
    @Override
    public List<SalaryArchiveDetail> getReport(String yearMonth, String companyId) {
        List<SalaryArchiveDetail> salaryArchiveDetailList=new ArrayList<SalaryArchiveDetail>(0);
        //根据companyId查询福利津贴
        QueryWrapper<Settings> settingsQueryWrapper=new QueryWrapper<Settings>();
        settingsQueryWrapper.eq("company_id",companyId);
        Settings settings = settingsMapper.selectOne(settingsQueryWrapper);
        //查询所有用户,遍历用户数据
        List<Map<String, Object>> mapList = userSalaryMapper.findPage(companyId);
        for (Map<String, Object> map : mapList) {
            SalaryArchiveDetail detail=new SalaryArchiveDetail();
            //循环用户，构造用户
            detail.setUser(map);
            //获取用户的社保数据
            ArchiveDetail socialInfo = feignClientService.getSocialInfo(detail.getUserId(), yearMonth);
            if(socialInfo!=null){
                detail.setSocialInfo(socialInfo);
            }
            //用户的考勤数据
            ArchiveMonthlyInfo atteInfo = feignClientService.getAtteInfo(detail.getUserId(), yearMonth);
            if(atteInfo!=null){
                detail.setAtteInfo(atteInfo);
            }
            //获取每个用户的薪资
            QueryWrapper<UserSalary> query=new QueryWrapper<UserSalary>();
            query.eq("user_id",detail.getUserId());
            UserSalary userSalary = userSalaryMapper.selectOne(query);
            if(userSalary!=null){
                detail.setUserSalary(userSalary);
            }
            //计算工资数据库数据有问题，可能会报空指针，数据源该对，不会出现该问题
            if(settings!=null){
                detail.calSalary(settings);
            }
            salaryArchiveDetailList.add(detail);
        }
        return salaryArchiveDetailList;
    }
}
